package com.chen.librarysystem.menu;

import com.chen.librarysystem.context.AppContext;
import com.chen.librarysystem.entry.Book;
import com.chen.librarysystem.log.LibraryLog;
import com.chen.librarysystem.service.BookService;
import com.chen.librarysystem.service.impl.BookServiceImpl;
import com.chen.librarysystem.util.PrintUtils;

import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;

public class UserBookMenu implements Menu {
    private static final Scanner sc = AppContext.getInstance().getScanner();
    private static final BookService bookService = new BookServiceImpl();

    @Override
    public void start() {
        while (true) {
            System.out.println("=========欢迎来到图书系统用户界面==========");
            System.out.println("1. 借阅图书");
            System.out.println("2. 归还图书");
            System.out.println("3. 显示自己的所有借阅图书");
            System.out.println("4. 显示所有图书");
            System.out.println("5. 图书系统用户管理界面(用户)");
            System.out.println("6. 退出图书系统用户界面");
            int type = PrintUtils.getIntInput(sc, "请输入您的选择:");
            switch (type) {
                case 1:
                    borrowBook();
                    break;
                case 2:
                    returnBook();
                    break;
                case 3:
                    getAllMyselfBorrowedBookByBorrower();
                    break;
                case 4:
                    displayBooks();
                    break;
                case 5:
                    LibraryLog.log(Level.INFO, "图书系统用户管理界面(用户)");
                    new UserManagementMenu().start();
                    break;
                case 6:
                    LibraryLog.log(Level.INFO, "退出图书系统用户界面(用户)！");
                    return;
                default:
                    System.out.println("输入错误，请重新输入！");
            }
        }
    }

    private void displayBooks() {
        List<Book> allBooks = bookService.getAllBooks();
        if (allBooks.isEmpty()) {
            LibraryLog.log(Level.INFO, "暂无数据");
            return;
        }
        PrintUtils.printBooks(allBooks);
        LibraryLog.log(Level.INFO, "共" + allBooks.size() + "条数据");
    }

    private void getAllMyselfBorrowedBookByBorrower() {
        List<Book> books = bookService.getBorrowedBooksByUser(AppContext.getInstance().getUserName());
        if (books.isEmpty()) {
            LibraryLog.log(Level.WARNING, "没有借阅图书");
            System.out.println("没有借阅图书");
            return;
        }
        PrintUtils.printBooks(books);
        LibraryLog.log(Level.INFO, "用户:" + AppContext.getInstance().getUserName() + "借阅了" + books.size() + "本图书！");
    }

    private void returnBook() {
        String title = PrintUtils.getInput("请输入要归还的图书名称：");
        bookService.returnBook(title, AppContext.getInstance().getUserName());
    }

    private void borrowBook() {
        String title = PrintUtils.getInput("请输入要借阅的图书名称：");
        bookService.borrowBook(title, AppContext.getInstance().getUserName());
    }
}
